package com.hxx.day7;

import org.junit.Test;

public class TestDemo4 {

    /**
     * 阻塞线程sleep
     * @param args
     */
    public static void main(String[] args) {
        //创建线程
        Runnable runnable = () -> {
            for (int index = 0; index < 10; index++) {
                String name = Thread.currentThread().getName();
                if ("线程1".equals(name)){
                    try {
                        //阻塞1秒
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(name +"---线程执行了"+index+"次");
            }
        };
        Thread thread1 = new Thread(runnable,"线程1");
        Thread thread2 = new Thread(runnable,"线程2");

        thread1.start();

        thread2.start();
    }

    @Test
    public void testYeid(){
        //创建线程
        Runnable runnable = () -> {
            for (int index = 0; index < 100; index++) {
                String name = Thread.currentThread().getName();
                System.out.println(name +"---线程执行了"+index+"次");
                if (index%2==0){
                   //阻塞
                   Thread.yield();
                }
            }
        };
        Thread thread1 = new Thread(runnable,"线程1");
        Thread thread2 = new Thread(runnable,"线程2");

        thread1.start();
        thread2.start();
    }
}
